<?php


namespace app\agent\controller\agent;

use app\agent\controller\Controller;
use app\agent\model\agent\Agent as AgentModel;
use app\agent\model\agent\User as AgentUserModel;
use app\agent\model\wxapp\Wxapp as WxappModel;
use think\App;
use think\Session;
use utils\Random;
use think\Config;
use think\Db;
use app\common\model\divide\Service;


class Agent extends Controller{
    /**
     * 查看代理商详情 1234
     */
    public function getAgentInfo(){
       $agentId=input('agent_id');
       $model = new AgentModel;
       $agentInfo=$model->getAgentInfo($agentId);
       return $this->fetch('getAgentInfo', [
           'list' => $agentInfo
       ]);
    }
    /*
     * 查看商户详情
     */
    public function getWxappInfo(){
        $wxapp_id=input('wxapp_id');
        $model = new WxappModel;
        $wxappInfo=$model->getWxappInfo($wxapp_id);
        return $this->fetch('getWxappInfo', [
            'list' => $wxappInfo
        ]);
    }
    /**
     * 代理合作伙伴列表
     */
    public function index(){
        $session = Session::get('yoshop_agent');
        $agent_id = $session['agent']['agent_id'];
        $model = new AgentModel;
        return $this->fetch('index', [
            'list' => $list = $model->getPartnerList($agent_id),
        ]);
    }
    /**
     * 修改当前管理员信息
     */
    public function renew(){
        $model = AgentUserModel::detail($this->agent['user']['agent_user_id']);
        if ($this->request->isAjax()) {
            if ($model->renew($this->postData('user'),$this->agent['user']['agent_user_id'])) {
                return $this->renderSuccess('修改成功','?s=/agent/passport/login');
            }
            return $this->renderError($model->getError() ?: '修改失败');
        }
        return $this->fetch('renew', compact('model'));
    }
    /*
     *获取代理分红
     */
    public function rivide(){
        $model = new Service();
        return $this->fetch('rivide', [
            'list' => $list = $model->getRivideList($this->request->param()),
            'statusList' => $model::getStatuslist(),
            'typeList' => $model::getTypelist(),

        ]);
    }
    /*
     *获取代理收入
     */
    public function getmoney(){
        $model = new Service();
        return $this->fetch('getmoney', [
            // 充值记录列表
            'list' => $list = $model->getMoneyList($this->request->param()),
        ]);
    }
    /*
     * 代理后台更新代理商账户名和密码
     */
    public function updateAgetInfo(){
        $salt=Random::alnum();
        $data['password']=input('post.password');
        if(empty($data['password'])){
            return ['code'=>-1,'msg'=>'新密码不能为空'];
        }
        if(!empty(input('post.username'))){
            $wxappData['user_name']=input('post.username');
        }else{
            return ['code'=>-1,'msg'=>'用户名不能为空'];
        }
        $wxappData['password']= md5(md5($data['password'].$salt.config('salt')));
        $agent_id=input('post.agent_id');
        //判断用户名称是否重复
        //判断当前要添加代理账号是否在所属品牌商下重复
        $session = Session::get('yoshop_agent');
        if(!empty(input('post.username'))){
            $agentOneInfo=Db::name('agent')->where(['pinpai_id'=>$session['agent']['pinpai_id'],'user_name'=>$wxappData['user_name']])->find();
            if($agentOneInfo&&($agentOneInfo['agent_id']!=$agent_id)){
                return ['code'=>-1,'msg'=>'该名称已被占用，请重新输入'];
            }
        }
        $wxappData['salt']=$salt;
        $agent=Db::name('agent')->where(['agent_id'=>$agent_id])->update($wxappData);
        $agentUser=Db::name('agent_user')->where(['agent_user_id'=>$agent_id])->update($wxappData);
        if($agent&&$agentUser){
            return ['code'=>1,'msg'=>'密码修改成功'];
        }
        return ['code'=>0,'msg'=>'修改失败'];
    }
}